package com.have.blog.admin.controller;

import com.have.blog.admin.annotion.avoidRepeatableCommit.AvoidRepeatableCommit;
import com.have.blog.admin.annotion.operationLogger.OperationLogger;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Delete;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.xo.service.admin.AdminCategoryMenuService;
import com.have.blog.xo.vo.CategoryMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 菜单管理接口
 */
@RestController
@RequestMapping("/categoryMenu")
@Api(value = "菜单信息相关接口", tags = {"菜单信息相关接口"})
@Slf4j
public class AdminCategoryMenuController {
    @Autowired
    private AdminCategoryMenuService categoryMenuService;

    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表", response = String.class)
    @GetMapping(value = "/getList")
    public String getList(@Validated({GetList.class}) @RequestBody CategoryMenuVo categoryMenuVo, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return categoryMenuService.getList(categoryMenuVo);
    }

    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表", response = String.class)
    @GetMapping(value = "/getAll")
    public String getAllList(@RequestParam(value = "keyword", required = false) String keyword) {
        return categoryMenuService.getAllList(keyword);
    }

    @ApiOperation(value = "获取所有二级菜单-按钮列表", notes = "获取所有二级菜单-按钮列表", response = String.class)
    @RequestMapping(value = "/getButtonAll", method = RequestMethod.GET)
    public String getButtonAll(@RequestParam(value = "keyword", required = false) String keyword) {

        return categoryMenuService.getAllButton(keyword);
    }

    @AvoidRepeatableCommit
    @OperationLogger(value = "增加菜单")
    @ApiOperation(value = "增加菜单", notes = "增加菜单", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody CategoryMenuVo categoryMenuVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return categoryMenuService.addCategoryMenu(categoryMenuVO);
    }

    @AvoidRepeatableCommit
    @OperationLogger(value = "测试菜单")
    @ApiOperation(value = "测试菜单", notes = "测试菜单", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody CategoryMenuVo categoryMenuVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return categoryMenuService.editCategoryMenu(categoryMenuVO);
    }

    /**
     * 如果是一级菜单，直接置顶在最前面，二级菜单，就在一级菜单内置顶
     */
    @OperationLogger(value = "置顶菜单")
    @ApiOperation(value = "置顶菜单", notes = "置顶菜单", response = String.class)
    @PostMapping("/stick")
    public String stick(@Validated({Delete.class}) @RequestBody CategoryMenuVo categoryMenuVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return categoryMenuService.stickCategoryMenu(categoryMenuVO);
    }

    @OperationLogger(value = "删除菜单")
    @ApiOperation(value = "删除菜单", notes = "删除菜单", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody CategoryMenuVo categoryMenuVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return categoryMenuService.deleteCategoryMenu(categoryMenuVO);
    }
}
